(function($){
    
    //编辑器对象
    var editor_this;    
    
    //编辑器对象
    var editor_object;
    
    //浮动值
    var box_index = 1;

    //方法
    var methods = {
        //html生成
        htmlbuild:function() {            
            //图片
            var _img = $('<i class="pic_img">图片</i>');            
            _img.bind('click', methods.htmlbuild_img)            
            
            //投票
            var _vote = $('<i class="vote">投票</i>');
            _vote.bind('click', methods.htmlbuild_vote);
            
            //留电话
            var _tel = $('<i class="tel">留电话</i>');
            _tel.bind('click', methods.htmlbuild_tel);
            
            //定时间
            var _time = $('<i class="time">定时间</i>');
            _time.bind('click', methods.htmlbuild_time);            
            
            //定地点
            var _addr = $('<i class="didian">定地点</i>');
            _addr.bind('click', methods.htmlbuild_addr);
            
            //选择器
            var _select = $('<div class="twitter_popedom"></div>');
            _select.html('权限选择：<select><option value="0" selected="selected">公开</option><option value="1">友邻可见</option></select>');
            _select.change(function(){
                editor_this.data('vote', $(this).val());
            })                                    
            
            //始终显示DIV
            editor_object._div.show();

            //取消离开焦点
            editor_object._edit.unbind('blur');
            
            //表情
            editor_object._emot.click(function(){
				methods.boxshow(this);
                box_index++;
                $(this).next('div').css('z-index', box_index);
            });
            
            //生成按钮
			editor_object._emot.after(_select);
            editor_object._emot.after(_addr);
            editor_object._emot.after(_time);
            editor_object._emot.after(_tel); 
            editor_object._emot.after(_vote);            
            editor_object._emot.after(_img);            
        },
        //生成img
        htmlbuild_img:function() {
            
            if(!methods.boxshow(this)) {
                return;                
            }
			
            //var file_post_url = '../plugin/twitter/upload.php';
			var file_post_url = '/handle.php?h=community_dynamic/add_pic';
            
            var box = $('<div class="pic_box" style="z-index:' + box_index + '">' +
            '<div class="jiantou"></div>' +
            '<div class="pic_box_top_box">' +
            '<a href="javascript:;" class="ico_off"></a>' +
            '</div>' +
            '<div>' +
            '<div class="pic_box_main">' +
			'<a href="javascript:;" class="file_up_box">' +
			'<span class="file_up_btn">添加图片</span>' +
			'<form action="' + file_post_url + '" method="post" enctype="multipart/form-data" target="upimg_iframe">' +
            '<input name="file" type="file" class="file_up_img" />' +
			'</form>' +
			'</a>' +
			'<div class="pic_box_main_text">（支持jpg、gif、png、bmp格式图片，不超过20M）</div>' +
            '</div>' +
            '</div>' +
			'<div class="upimg_loading" style="display:none;"></div>' +
            '<iframe name="upimg_iframe" src="" width="1" height="1" frameborder="0"></iframe>' +
            '</div>');            
            
            //关闭
            var _close = $('a:eq(0)', box);
            _close.click(function(){
                box.remove(); 
				              
            });
			
			//iframe
            var _iframe = $('iframe', box);
			
			//loading
			var _loading = $('.upimg_loading', box);                        
			
			//form
			var _form = $('form', box);
			_form.submit(function(){
				_iframe.load(function(){
					_loading.hide();
					
					var _img_ary = common_fun_obj.str_to_json(_iframe.contents().find('body').text());
					
					_loading.after('<img src="' + _img_ary.img + '" width="80" />');
					editor_this.data('img', _img_ary.img);	
                    									
					editor_object._edit.comment('insert', '[img=' + _img_ary.name + ']' + _img_ary.img + '[/img]');															
				});
			});                        			    
			
			//上传
            var _file = $('input:file', box);
            _file.change(function(){								
                $(this).parents('.pic_box_main').hide();				
				_loading.show();
				_form.submit();                                        							
            });                  
                            
            
            $(this).after(box);
        },
        //生成投票
        htmlbuild_vote:function() {
            
            if(!methods.boxshow(this)) {
                return;                
            }                        
            
            var box = $('<div class="vote_box" style="z-index:' + box_index + '">' +
            '<div class="jiantou"></div>' +
            '<div class="vote_box_top_box"><a href="javascript:;" class="ico_off"></a></div>' +
            '<div class="vote_box_main">' +
            '<dl><dd>' +
            '<li><input type="text" class="box"/></li>' +
            '<li><input type="text" class="box"/></li>' +
            '<li><input type="text" class="box"/></li>' +            
            '<li><a href="javascript:;">+添加选项</a>&nbsp;<a href="javascript:;">-减少选项</a></li>' +
            '</dd><dt style="padding-top:12px;">投票选项:</dt>' +
            '<dd>' +
            '<input type="radio" checked="checked" name="vote" value="1" />单选&nbsp;&nbsp;' +
            '<input type="radio" name="vote" value="2" />多选</dd><dt>投票方式:</dt>' +
            '<dd><input type="button" class="add_pic2" value="确定"></dd><dt></dt></dl></div></div></div>');
            
            //关闭
            var _close = $('a:eq(0)', box);
            _close.click(function(){
                box.remove();
                editor_this.data('vote', '');                
            });
            
            //添加
            var _add = $('a:eq(1)', box);
            _add.click(function(){                
                $(this).parent().before('<li><input type="text" class="box" /></li>');                
                if($(this).parents('dd').find('input').length > 4) {
                    _add.hide();                                    
                }
                
                if($(this).parents('dd').find('input').length > 0) {
                    _remove.show();
                }                                                        
            });
            
            //删除
            var _remove = $('a:eq(2)', box);
            _remove.click(function(){
                $(this).parent().prev().remove();
                if($(this).parents('dd').find('input').length < 2) {
                    _remove.hide();                                    
                }
                
                if($(this).parents('dd').find('input').length > 0) {
                    _add.show();
                }
            });
            
            //确定
            var _btn = $('input:button', box);
            _btn.click(function(){
                
                var flag = false;                
                
                //获取值
                var _val = [];
                $('input:text', box).each(function(){
                    if($(this).val()) {
                        flag = true;
                    }
                    
                    _val.push($(this).val());
                });
                
                //一个值都没有
                if(!flag) {
                    $('input:text', box).each(function(){
                        if($(this).val() == '') {
                            $(this).focus();
                            return false;
                        }
                    })
                    return;
                }
                
                var _type = 1;                
                $('input:radio', box).each(function(){                    
                    if($(this).attr('checked')) {
                        _type = $(this).val();
                    }
                });                                
                                
                editor_this.data('vote', {val:_val, type:_type});
                
                //锁定标签
                _add.remove();
                _remove.remove();
                $('input:text', box).attr('readonly', 'readonly');
                $('input:radio', box).attr('disabled', 'disabled');
                $(this).remove();
            })
            
            $(this).after(box);            
        },
        //留电话
        htmlbuild_tel:function() {
            
            if(!methods.boxshow(this)) {
                return;                
            }                        
            
            var box = $('\
            <div class="tel_box" style="z-index:' + box_index + '">\
            <div class="jiantou"></div>\
            <div class="tel_box_top_box"><a href="javascript:;" class="ico_off"></a></div>\
            <div class="tel_box_main">\
            <dl>联系电话：<input type="text" class="box"/></dl>\
            <dl><dt></dt><input type="button" class="add_pic2" value="确认"></dl>\
            </div></div>');
            
            //关闭
            var _close = $('a', box);
            _close.click(function(){                
                box.remove();
                editor_this.data('tel', '');                
            });
            
            //值
            var _input = $('input:text', box);
            
            //确定
            var _btn = $('input:button', box);
            _btn.click(function(){                
                if(_input.val() == '') {
                    _input.focus();
                    return;
                }
                
                if(!/^(1[3,5,8,7]{1}[\d]{9})|(((400)-(\d{3})-(\d{4}))|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{3,7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/.test(_input.val())) {
                    _input.css('border-color', 'red').focus();                    
                    return;
                }
                                
                editor_this.data('tel', _input.val());
                $(this).remove();
                _input.attr('readonly', 'readonly').css('border-color', '#ccc');
            });
            
            $(this).after(box);
        },
        //定时间
        htmlbuild_time:function() {
            if(!methods.boxshow(this)) {
                return;                
            }
            
            var box = $('\
            <div class="time_box" style="z-index:' + box_index + '">\
            <div class="jiantou"></div>\
            <div class="tel_box_top_box"><a href="javascript:;" class="ico_off"></a></div>\
            <div class="tel_box_main">\
            <dl>请选择时间：<input type="text" id="twitter_time" class="box" readonly="readonly" /></dl>\
            <dl><dt></dt><input type="button" class="add_pic2" value="确认"></dl>\
            </div></div>');
            
            //关闭
            var _close = $('a', box);
            _close.click(function(){                
                box.remove();
                editor_this.data('time', '');                        
            });
            
            //值
            var _input = $('input:text', box);
            
            //确定
            var _btn = $('input:button', box);
            _btn.click(function(){
                if(_input.val() == '') {
                    _input.css('border-color', 'red').focus();
                    return;
                }
                
                _input.css('border-color', '#ccc').attr('readonly', 'readonly');
                $(this).remove();                
                editor_this.data('time', _input.val());
            });
            
            $(this).after(box);
        },
        //定地点
        htmlbuild_addr:function() {
            if(!methods.boxshow(this)) {
                return;                
            }
            
            var box = $('\
            <div class="addr_box" style="z-index:' + box_index + '">\
            <div class="jiantou"></div>\
            <div class="tel_box_top_box"><a href="javascript:;" class="ico_off"></a></div>\
            <div class="tel_box_main">\
            <dl>填写地点：<input type="text" class="box"/></dl>\
            <dl><dt></dt><input type="button" class="add_pic2" value="确认"></dl>\
            </div></div>');
            
            //关闭
            var _close = $('a', box);
            _close.click(function(){                
                box.remove();
                editor_this.data('addr', '');                        
            });
            
            //值
            var _input = $('input:text', box);
            
            //确定
            var _btn = $('input:button', box);
            _btn.click(function(){
                if(_input.val() == '') {
                    _input.css('border-color', 'red').focus();
                    return;
                }
                
                _input.css('border-color', '#ccc').attr('readonly', 'readonly');
                $(this).remove();                
                editor_this.data('addr', _input.val());
            });
            
            $(this).after(box);
        },
        //窗体显示
        boxshow:function(obj) {
			
			if(editor_object._label.css('display') != 'none') {
				editor_object._edit.focus().click();				
				editor_object._label.hide();
			}
			        
            box_index++;
            
            if($(obj).next('div[class!="twitter_popedom"]').html()) {
                $(obj).next('div').css('z-index', box_index);
                return false;
            }
            
            return true;        
        }
    }		                
    
    //twitter插件
    $.fn.comment.twitter = function(args){    
        editor_this = this;
        editor_object = args;
        methods.htmlbuild();
    }		
	
})(jQuery);